//页面初始化
$(document).ready(function(){
	jqGridBuild();
	//查询按钮
	$("#searchForm").submit(function() {
		commonSearch("dataTable","searchForm");
	});
	//页面自适应
	$(window).bind('resize', function() {
		var width = $('.jqGrid_wrapper').width();
		$('#dataTable').setGridWidth(width);
	});
});
//表格初始化
function jqGridBuild(){
	var defaultColNames =['用户帐号', '姓名', '性别', '部门', '手机','办公电话', '电子邮件', '主键'];
	var defaultColModel =[ 
		{name : 'userAcct',index :'user_acct',width : 140}, 
		{name : 'userName',index : 'user_name',width : 90}, 
		{name : 'sex',index : 'sex',width : 90}, 
		{name : 'departmentName',index : 'department_code',width : 150}, 
		{name : 'mobile',index : 'mobile',width : 160,sorttype : "int"}, 
		{name : 'telephone',index : 'telephone',width : 180}, 
		{name : 'email',index : 'email',width : 120}, 
		{name : 'id',index : 'id',hidden:true}
	];
	//数据表格显示
	$("#dataTable").jqGridShow({
		caption: "帐户列表",
		url : contextPath+'/user/queryUserList.do',
		postData : getSearchFormJson("searchForm"),
		colNames : defaultColNames,
		colModel : defaultColModel,
		pager : "#tablePager",
        viewrecords: true,
        hidegrid: false,
        multiselect: true,
        rownumbers: false,
        shrinkToFit: false,
        autoScroll: true,
        height: 360,
        subGrid:false,//子表格显示
		serializeGridData: function(postData) {
		    return JSON.stringify(postData);
		},
        gridComplete: function () {
        	// 在此事件中循环为每一行添加修改和删除链接
            var ids = $("#dataTable").jqGrid('getDataIDs');
            for (var i = 0; i < ids.length; i++) {
                var id = ids[i];
                var model = $("#dataTable").jqGrid('getRowData',id);
                var operaHTML = getModifyBtn(model);
                // 这里的onclick就是调用了上面的javascript函数
                // Modify(id)
                $("#dataTable").jqGrid('setRowData', ids[i], {opera: operaHTML});
            }
        },
        ondblClickRow: function (id) {
        	console.log("双击事件，获取ID："+id);
        	return null;
        }
	});
	//添加按钮
	$("#dataTable").jqGrid('navGrid', '#tablePager', {
		edit: true,
		add: true,
		del: true,
		search: true,
		addfunc: function(){	
			add();
		}, 
		editfunc: function(rowId){
			edit();
		},
		delfunc: function(rowId){
			del();
		},
		searchfunc: function(){
			layer.open({
				type: 1,
				title: '查询条件',
				skin: 'layui-layer-lan',
				fixed:true,
				area: ['700px', '450px'],
				content: $('#searchCondition'),
				end: function(){
				}
			});
		},
		height: 200,
		reloadAfterSubmit: true
	});
}
//获取操作单元格HTML
function getModifyBtn(model){
	var operaHTML = "";
//	if(model.status == "初始化"){
//		operaHTML = operaHTML + "<a title='冻结' class='green previewInfo' href='#' onclick='freeznRepoTask(\""+model.repoTaskCode+"\")'>冻结数据</a>&nbsp; ";
//	} else if(model.status == "上报失败"){
//		operaHTML = operaHTML + "<a title='解冻数据' class='green previewInfo' href='#' onclick='unFreeznRepoTask(\""+model.repoTaskCode+"\")'>解冻数据</a>&nbsp; ";
//	} 
	return operaHTML;
}
//添加
function add() {
	layer.open({
	  type: 2,
	  title: '新增帐户信息',
	  skin: 'layui-layer-lan',
	  area: ['100%', '100%'],
	  content: contextPath+'/user/toUpdateUser.do',
	  maxmin: true
	});
}
//编辑
function edit() {
	var rows = $('#dataTable').jqGrid('getGridParam','selarrrow');
	if(rows && rows.length == 1){
		var user = $('#dataTable').jqGrid('getRowData',rows[0]);
		layer.open({
		  type: 2,
		  title: '修改帐户信息',
		  skin: 'layui-layer-lan',
		  area: ['100%', '100%'],
		  content: contextPath+'/user/toUpdateUser.do?userId=' + user.id,
		  maxmin: true
		});
	} else {
		layer.alert('请选择记录!');
		return false;
	}
}

function del() {
	var rows = $('#dataTable').jqGrid('getGridParam','selarrrow');
	if(rows && rows.length == 1){
		layer.confirm('确认删除吗？', {
		    btn: ['确定','取消'], //按钮
		}, function(){
			var user = $('#dataTable').jqGrid('getRowData',rows[0]);
			LocalAjax({
				url : contextPath+"/user/delUser.do",
				data:JSON.stringify({"id":user.id}),
				async : false,
				success : function(data) {
					var result = eval(data);
					if (result.resultCode == '1') {
	            		layer.alert('删除成功', function(i){
	            			$("#searchForm").submit();
	            			layer.close(i);
	            		});
	            	}else{
	            		var message = result.message;
						if (message != "") {
							layer.alert(message);
						} else {
							layer.alert('删除失败');
						}
	            	}
				}
			});
		}, function(){
		});
	} else {
		layer.alert('请选择记录!');
		return false;
	}
}
//角色权限
function doActor() {
	var rows = $('#dataTable').jqGrid('getGridParam','selarrrow');	
	if(rows && rows.length == 1){
		var user = $('#dataTable').jqGrid('getRowData',rows[0]);
		layer.open({
		  type: 2,
		  title: '角色选择',
		  area: ['100%', '100%'],
		  skin: 'layui-layer-lan',
		  content: contextPath+'/user/showAction.do?userId=' + user.id,
		  maxmin: true
		});
	} else {
		layer.alert('请选择记录!');
		return false;
	}
	//$(".modal-body").load("showActor.do", "user.id=" + userId);
}
